#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <string.h>

#define DEVICE_PATH "/dev/mycdev_simple0"
#define PROPERTY_PATH "/sys/devices/virtual/mycdev_class/mycdev_simple0/my_property"
#define defaultMode 0
#define IOCTL_MODE_SWITCH _IO('M',0)


int main() {
    // 以下 mode=0
    // printf("The default mode mode is 0: \n");
    int fd;
    char buffer[256];
    // 打开设备文件
    fd = open(DEVICE_PATH, O_RDWR);
    if (fd < 0) {
        perror("Failed to open the device");
        return -1;
    }


    // 写入字符串
    char write_str[] = "Hello, world!";
    printf("Writing to the device: %s\n", write_str);
    if (write(fd, write_str, strlen(write_str)) < 0) {
        perror("Failed to write to the device");
        close(fd);
        return -1;
    }

    // 读取字符串
    memset(buffer, 0, sizeof(buffer));
    printf("Reading from the device: ");
    if (read(fd, buffer, sizeof(buffer)) < 0) {
        perror("Failed to read from the device");
        close(fd);
        return -1;
    }

    printf("%s\n", buffer);


 
    // 使用 "echo" 操作写入数据到设备属性
    const char *echo_str = "This is an echo";
    printf("Using echo operation to write data: %s\n", echo_str);
    FILE *prop_file = fopen(PROPERTY_PATH, "w");
    fprintf(prop_file,"%s",echo_str);
    if (prop_file == NULL) {
        perror("Failed to open property file");
        close(fd);
        return -1;
    }
    fclose(prop_file);

    // 从设备属性读出
    char read_str[256];
    FILE *read_file = fopen(PROPERTY_PATH, "r");
    if(read_file == NULL) {
        perror("Failed to open property file");
        close(fd);
        return -1;
    }
    // fscanf(read_file, "%s", read_str);
    fgets(read_str,sizeof(read_str),read_file);
    printf("read from property file: %s\n", read_str);
    fclose(read_file);

    // 关闭设备文件
    // close(fd);




    printf("\n");
    printf("reverse mode:\n");
    printf("\n");
    ioctl(fd,IOCTL_MODE_SWITCH);

    
    // 以下 mode=1
    // fd = open(DEVICE_PATH, O_RDWR);
    // if (fd < 0) {
    //     perror("Failed to open the device");
    //     return -1;
    // }



    // 写入字符串
    char write_str2[] = "Hello, world2!";
    printf("Writing to the device: %s\n", write_str2);
    if (write(fd, write_str2, strlen(write_str2)) < 0) {
        perror("Failed to write to the device");
        close(fd);
        return -1;
    }

    // 读取字符串
    memset(buffer, 0, sizeof(buffer));
    printf("Reading from the device: ");
    if (read(fd, buffer, sizeof(buffer)) < 0) {
        perror("Failed to read from the device");
        close(fd);
        return -1;
    }
    printf("%s\n", buffer);
    // 使用 "echo" 操作写入数据到设备属性
    const char *echo_str2 = "This is an echo2";
    printf("Using echo operation to write data: %s\n", echo_str2);
    FILE *prop_file2 = fopen(PROPERTY_PATH, "w");
    fprintf(prop_file,"%s",echo_str2);
    if (prop_file2 == NULL) {
        perror("Failed to open property file");
        close(fd);
        return -1;
    }
    fclose(prop_file2);

    // 从设备属性读出
    char read_str2[256];
    FILE *read_file2 = fopen(PROPERTY_PATH, "r");
    if(read_file2 == NULL) {
        perror("Failed to open property file");
        close(fd);
        return -1;
    }
    // fscanf(read_file, "%s", read_str);
    fgets(read_str2,sizeof(read_str2),read_file2);
    printf("read from property file: %s\n", read_str2);
    fclose(read_file2);

    // 关闭设备文件
    close(fd);

    return 0;
}

